﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace igflow.Meshes
{
    class igTextMesh : Mesh
    {
        internal short indCount = 0;
        internal short vCount = 0;
        string text = "";
        public igTextMesh(igFont font, float w, float h, string text, Color color)
        {
            Textures = new List<Texture2D>();
            Textures.Add(font.texture);
            vertexDeclaration = new VertexDeclaration(World.Graphics.GraphicsDevice, VertexPositionColorTexture.VertexElements);

            Vertices = new VertexPositionColorTexture[text.Length * 4];
            Indexes = new short[text.Length * 6];

            // Fill in texture coordinates to display full texture
            // on quad

            Vector3 Position = Vector3.Zero;
            string[] split = text.Split('\n');
            foreach (string t in split)
            {
                foreach (char c in t)
                {
                    if (font.Characters.ContainsKey(c))
                    {
                        float uvScale = 1 / (float)font.texture.Width;
                        float drawW = w;
                        AddCharQuad(font.Characters[c], uvScale, Position, drawW);
                        Position.X += w;
                    }
                    else
                    {
                        // a space character
                        Position.X += w;
                    }
                }
                Position.X = 0;
                Position += new Vector3(0, -w,0);
            }

            for (int i = 0; i < Vertices.Length; i++)
            {
                Vertices[i].Color = color;
            }
        }

        internal void AddCharQuad(igFont.character character, float scale, Vector3 start , float drawW)
        {
            float w = drawW;
            float h = drawW;
            Vector2 textureUpperLeft = new Vector2(
                        character.x * scale,
                        character.y * scale);

            Vector2 textureUpperRight = new Vector2(
                (character.x * scale) + (character.width * scale),
                character.y * scale);


            Vector2 textureLowerLeft = new Vector2(character.x * scale,
                (character.y * scale) + (character.height * scale));


            Vector2 textureLowerRight = new Vector2(
                (character.x * scale) + (character.width * scale),
                (character.y * scale) + (character.height * scale));

            // Set the position and texture coordinate for each
            // vertex

            Vertices[vCount + 0].Position = new Vector3(start.X, start.Y, 0);
            Vertices[vCount + 0].TextureCoordinate = textureUpperLeft;
            //Vertices[vCount + 0].Color = Color.Blue;

            Vertices[vCount + 1].Position = new Vector3(start.X + w, start.Y, 0);
            Vertices[vCount + 1].TextureCoordinate = textureUpperRight;
            //Vertices[vCount + 1].Color = Color.White;

            Vertices[vCount + 2].Position = new Vector3(start.X, start.Y - h, 0);
            Vertices[vCount + 2].TextureCoordinate = textureLowerLeft;
           // Vertices[vCount + 2].Color = Color.Red;

            Vertices[vCount + 3].Position = new Vector3(start.X + w, start.Y -h, 0);
            Vertices[vCount + 3].TextureCoordinate = textureLowerRight;
           // Vertices[vCount + 3].Color = Color.Green;


            // Set the index buffer for each vertex, using
            // clockwise winding
            Indexes[indCount + 0] = (short)(vCount + 0);
            Indexes[indCount + 1] = (short)(vCount + 1);
            Indexes[indCount + 2] = (short)(vCount + 2);
            Indexes[indCount + 3] = (short)(vCount + 2);
            Indexes[indCount + 4] = (short)(vCount + 1);
            Indexes[indCount + 5] = (short)(vCount + 3);

            indCount += 6;
            vCount += 4;
        }
    }
}
